package com.yxuan.springboot.service.impl;

import com.yxuan.springboot.mapper.TestOptionsMapper;
import com.yxuan.springboot.mapper.TestQuestionsMapper;
import com.yxuan.springboot.mapper.TestTypeDetailsMapper;
import com.yxuan.springboot.model.TestOptions;
import com.yxuan.springboot.model.TestQuestions;
import com.yxuan.springboot.service.TestQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author zchao
 *    TestQuestions业务层实现类
 * @date 2023-05-16 14:22:57
 */
@Service
@Transactional
public class TestQuestionsServiceImpl extends BaseServiceImpl<TestQuestions> implements TestQuestionsService {


    @Autowired
    private TestQuestionsMapper testQuestionsMapper;

    @Autowired
    private TestOptionsMapper testOptionsMapper;

    @Autowired
    private TestTypeDetailsMapper testTypeDetailsMapper;
//    新增题目，同时添加选项
    @Override
    public HashMap<String,Object> addQuestion(Map map) throws Exception {
        HashMap<String,Object> result = new HashMap<>();
        TestQuestions testQuestions = new TestQuestions();
        testQuestions.setContent((String) map.get("content"));
        testQuestions.setQuestionPic((String) map.get("questionPic"));
        testQuestions.setTypeId((Integer) map.get("typeId"));
        testQuestions.setUserId((String) map.get("userId"));
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        testQuestions.setCreateTime(timestamp);
        testQuestions.setUpdateTime(timestamp);
//        新增题目
        testQuestionsMapper.insert(testQuestions);
        TestQuestions testQuestions1 = testQuestionsMapper.queryObjectByPramas(testQuestions);
//        TestTypeDetails testTypeDetails = testTypeDetailsMapper.queryObjectById(String.valueOf(testQuestions.getId()));
//        testTypeDetails.setTopicNum(testTypeDetails.getTopicNum()+1);
//        testTypeDetailsMapper.update(testTypeDetails);
//        添加题目选项optionName/optionScore/optionContent/userid
        List<Map> options = (List<Map>) map.get("options");
        for (Map m : options){
            TestOptions options1 = new TestOptions();
            options1.setOptionName((String) m.get("optionname"));
            options1.setOptionContent((String) m.get("optioncontent"));
            String optionscore = m.get("optionscore").toString();
            float v = Float.parseFloat(optionscore);
            options1.setQuestionId(testQuestions1.getId());
            options1.setOptionScore(v);
            options1.setCreateTime(timestamp);
            options1.setUpdateTime(timestamp);
            options1.setUserid((String) map.get("userId"));
            testOptionsMapper.insert(options1);
        }
        result.put("addquestion","success");
        return result;
    }


//    TODO 删除题目，未完成：删除后的 version 以及 resords 的处理

    @Override
    public HashMap<String, Object> deleteQuestion(TestQuestions testQuestions) throws Exception {
        HashMap<String,Object> result = new HashMap<>();
//        删除题目
        testQuestionsMapper.delete(testQuestions);
        TestOptions options = new TestOptions();
        options.setQuestionId(testQuestions.getId());
//        删除选项
        testOptionsMapper.delete(options);
        result.put("delete","success");
        return result;
    }


}
